Public Class Table
    Inherits ObjectBase

    Private msName As String
    Private mlOID As Long
    Private mlOwnerID As Long
    Private mlDatabaseID As Long
    Private mlDatabaseRevID As Long
    Private msDesc As String
    Private msRevDate As String = Now

    Private msFieldsHash As String
    Private msIndexesHash As String

    Public Sub New()
        MyBase.New()
    End Sub

    Public Sub New(ByVal lID As Long)
        MyBase.New(lID)
    End Sub

    Public Sub New(ByVal lID As Long, ByVal lRevID As Long)
        MyBase.New(lID, lRevID)
    End Sub

#Region "Properties"
    Public Property Name() As String
        Get
            Return msName
        End Get
        Set(ByVal value As String)
            msName = value
        End Set
    End Property

    Public Property OID() As Long
        Get
            Return mlOID
        End Get
        Set(ByVal value As Long)
            mlOID = value
        End Set
    End Property

    Public Property OwnerID() As Long
        Get
            Return mlOwnerID
        End Get
        Set(ByVal value As Long)
            mlOwnerID = value
        End Set
    End Property

    Public Property DatabaseID() As Long
        Get
            Return mlDatabaseID
        End Get
        Set(ByVal value As Long)
            mlDatabaseID = value
        End Set
    End Property

    Public Property DatabaseRevID() As Long
        Get
            Return mlDatabaseRevID
        End Get
        Set(ByVal value As Long)
            mlDatabaseRevID = value
        End Set
    End Property

    Public Property RevDate() As String
        Get
            Return msRevDate
        End Get
        Set(ByVal value As String)
            msRevDate = value
        End Set
    End Property

    Public Property Desc() As String
        Get
            Return msDesc
        End Get
        Set(ByVal value As String)
            msDesc = value
        End Set
    End Property

    Public Property FieldsHash() As String
        Get
            Return msFieldsHash
        End Get
        Set(ByVal value As String)
            msFieldsHash = value
        End Set
    End Property

    Public Property IndexesHash() As String
        Get
            Return msIndexesHash
        End Get
        Set(ByVal value As String)
            msIndexesHash = value
        End Set
    End Property
#End Region

#Region "database"
    Public Sub Load()
        Dim oObject As New DBTable

        If mlRevID > -1 Then
            oObject.LoadRev(Me, mlRevID)
        Else
            oObject.Load(Me)
        End If
    End Sub

    Public Sub Save(Optional ByVal bForceAdd As Boolean = False)
        Dim oObject As New DBTable

        If bForceAdd = False AndAlso mbObjectExists = True Then
            oObject.Update(Me)
        Else

            Dim oNextID As New DBNextIDValue("Table")
            Dim lNextID As Long = oNextID.NextValue

            Me.ID = lNextID

            If bForceAdd = False Then
                Me.RevID = 0
            End If

            Me.CreateDate = Now
            oObject.Add(Me)
            mbObjectExists = True
        End If
    End Sub

    Public Sub Rev( _
                ByVal sFieldsHash As String, _
                ByVal sIndexesHash As String)

        Dim oObject As New DBTable

        Me.RevDate = Now

        If Me.FieldsHash > "" Then
            Me.FieldsHash = sFieldsHash
            Me.IndexesHash = sIndexesHash

            mlRevID = mlRevID + 1

            Me.CreateDate = Now
            oObject.Add(Me)

        Else
            Me.FieldsHash = sFieldsHash
            Me.IndexesHash = sIndexesHash

            oObject.Update(Me)
        End If

        mbObjectExists = True
    End Sub

    Public Sub ForceRev()
        Dim oObject As New DBTable

        Me.FieldsHash = ""
        Me.IndexesHash = ""

        Scan()
    End Sub

    Public Sub Delete()
        Dim oObject As New DBTable

        oObject.Delete(Me)
        mbObjectExists = False
    End Sub
#End Region

    Public Function Scan() As Boolean
        Dim bTableHasChanged As Boolean = False

        '---Scan text of this table 
        Dim oDatabase As New SQLAutoDocLib.Database(mlDatabaseID)
        oDatabase.Load()

        Dim oServer As New SQLAutoDocLib.Server(oDatabase.ServerID)
        oServer.Load()

        Dim oLogin As New SQLAutoDocLib.Login(mlOwnerID)
        oLogin.Load()

        Dim oFieldScanner As New DBScanFields(oServer, oDatabase, oLogin, Me)
        Dim oIndexScanner As New DBScanIndexes(oServer, oDatabase, oLogin, Me)

        Dim sFieldsHash As String = ""
        Dim sIndexesHash As String = ""

        '---Get list of fields and checksum
        Dim oFieldList As ArrayList = Nothing
        oFieldScanner.Scan(oFieldList, sFieldsHash)

        '---Get list of indexes and checksum
        Dim oIndexList As ArrayList = Nothing
        oIndexScanner.Scan(oIndexList, sIndexesHash)

        '---If checksum has changed, then rev table
        If (sFieldsHash <> msFieldsHash) _
            OrElse (sIndexesHash <> msIndexesHash) Then

            bTableHasChanged = True

            '---Table has revved
            Me.Rev(sFieldsHash, sIndexesHash)

            '---Save field list
            For Each oField As Field In oFieldList
                oField.TableRevID = Me.RevID
                oField.Save()
            Next

            '---Save index list
            For Each oIndex As Index In oIndexList
                oIndex.TableRevID = Me.RevID
                oIndex.Save()
            Next
        End If

        Return bTableHasChanged
    End Function
End Class
